<?php

namespace App\Services\Calculation;

use App\Enums\CalculationEnums;
use App\Services\BaseServices;

class CalculationServices extends BaseServices
{
    /**
     * 碳循环算法
     * @param $weight
     * @param $height
     * @param $age
     * @param $bodyFatRatio
     * @param $strength
     * @param $sex
     * @param  int  $type
     * @return array
     */
    public function calculation($weight, $height, $age, $bodyFatRatio, $strength, $sex, $type = 1)
    {
        $BMR = $this->BMR($weight, $height, $age, $sex);
        $bmr = bcmul($BMR, CalculationEnums::COEFFICIENT_OF_MOTION[$strength], 2);
        $dayNeed = bcmul($bmr, CalculationEnums::EMBRYO_TYPE[$type], 2);
        $dayNeed = bcsub($bmr, $dayNeed, 2);
        $gap = bcsub($bmr,$dayNeed,2);
        $data = [
            "BMR" => "$BMR Kcal",
            "dailyConsumption" => "$bmr Kcal",
            "dayNeed" => "$dayNeed Kcal",
            "gap" => "$gap Kcal",
            "medium" => $this->mediumCarbon($weight, $bodyFatRatio, $dayNeed),
            "max" => $this->highCarbon($weight, $bodyFatRatio, $dayNeed),
            "low" => $this->lowCarbon($weight, $bodyFatRatio, $dayNeed),
        ];
        return $data;
    }

    /**
     * 基础代谢计算
     * @param $weight
     * @param $height
     * @param $age
     * @param $sex  1：男 2：女
     * @return float|int
     */
    public function BMR($weight, $height, $age, $sex = 1)
    {
        if ($sex == 1) {
            $tizhong = bcmul(13.7, $weight, 2);
            $shengao = bcmul(5, $height, 2);
            $shengaotizhong = bcadd(66, $tizhong);
            $shengaotizhong = bcadd($shengaotizhong, $shengao, 2);
            $nianling = bcmul(6.8, $age, 2);
            $BMR = bcsub($shengaotizhong, $nianling, 2);
        } else {
            $BMR = 655 + (9.6 * $weight) + (1.7 * $height) - (4.7 * $age);
        }
        return $BMR;
    }

    /**
     * 中碳日所需
     * @param $weight
     * @param $bodyFatRatio
     * @param $dayNeed
     * @return array
     */
    public function mediumCarbon($weight, $bodyFatRatio, $dayNeed)
    {
        $pound = bcmul($weight, 2.2046226, 2);
        $leanWeight = bcsub($pound, bcmul($pound, $bodyFatRatio, 2), 2);
        $eat['carbohydrates'] = bcmul($pound, 1.25, 2);//中碳日碳水
        $eat['protein'] = bcmul($leanWeight, 1, 2);//中碳日蛋白质
        $tanOrDan = bcadd($eat['carbohydrates'], $eat['protein'], 2);
        $tanOrDanKcl = bcmul($tanOrDan, 4, 2);
        $fatKcl = bcsub($dayNeed, $tanOrDanKcl, 2);
        $fat = bcdiv($fatKcl, 9, 2);
        $eat['fat'] = $fat;//中碳日脂肪
        return $eat;
    }

    /**
     * 高碳日所需
     * @param $weight
     * @param $bodyFatRatio
     * @param $dayNeed
     * @return array
     */
    public function highCarbon($weight, $bodyFatRatio, $dayNeed)
    {
        $zhongArr = $this->mediumCarbon($weight, $bodyFatRatio, $dayNeed);
        $eat['carbohydrates'] = bcmul($zhongArr['carbohydrates'], 1.75, 2);//中碳日碳水
        $eat['protein'] = $zhongArr['protein'];//中碳日蛋白质
        $eat['fat'] = bcsub($zhongArr['fat'], bcmul($zhongArr['fat'], 0.5, 2));//中碳日脂肪
        return $eat;
    }

    /**
     * 低碳日所需
     * @param $weight
     * @param $bodyFatRatio
     * @param $dayNeed
     * @return array
     */
    public function lowCarbon($weight, $bodyFatRatio, $dayNeed)
    {
        $zhongArr = $this->mediumCarbon($weight, $bodyFatRatio, $dayNeed);
        $eat['carbohydrates'] = bcmul($zhongArr['carbohydrates'], 0.25, 2);//中碳日碳水
        $eat['protein'] = $zhongArr['protein'];//中碳日蛋白质
        $eat['fat'] = bcadd($zhongArr['fat'], bcmul($zhongArr['fat'], 1.5, 2), 2);//中碳日脂肪
        return $eat;
    }
}
